package io.github.syske.springbootjdbcmybatisdemo.controller;

import io.github.syske.springbootjdbcmybatisdemo.common.CustomIdGenerator;
import io.github.syske.springbootjdbcmybatisdemo.common.DBDirectorService;
import io.github.syske.springbootjdbcmybatisdemo.common.GetOPCodeUtil;
import io.github.syske.springbootjdbcmybatisdemo.common.ResultObject;
import io.github.syske.springbootjdbcmybatisdemo.dao.*;
import io.github.syske.springbootjdbcmybatisdemo.entity.Locate;
import io.github.syske.springbootjdbcmybatisdemo.entity.TempOp;
import io.github.syske.springbootjdbcmybatisdemo.entity.User;
import io.github.syske.springbootjdbcmybatisdemo.entity.vo.MallVO;
import io.github.syske.springbootjdbcmybatisdemo.entity.vo.SttVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
public class TestController {

    @Autowired
    DBDirectorService dbDirectorService;
    @Autowired
    TempOpDao tempOpDao;

    @Autowired
    TPDao tpDao;

    @Autowired
    LocateDao locateDao;

    @Autowired
    UserDao userDao;

    @Autowired
    GetOPCodeUtil getOPCodeUtil;
    @Autowired
    MallDao mallDao;

    @RequestMapping("api/outStorageBak")
    public Object outStorage(@RequestBody Map<String,Object> param, HttpSession session){
        System.out.println("现在调的是整盘出库接口...");
        Object out_from = param.get("out_from");
        if(out_from!=null && out_from.toString().equals("2")){
            if(param.get("password")!=null && !param.get("password").equals("")){
                User user = (User) session.getAttribute("user");
                if(user!=null){
                    Map<String,Object> map = new HashMap<>();
                    map.put("user_name",user.user_name);
                    map.put("user_password",param.get("password").toString());
                    List<User> list = userDao.getUserByName(map);
                    if(list==null || list.size()==0){
                        ResultObject oo =new ResultObject();
                        oo.code=401;
                        oo.message="密码输入有误";
                        return oo;
                    }
                }else {
                    ResultObject oo =new ResultObject();
                    oo.code=403;
                    oo.message="session过期，请重新登录";
                    return oo;
                }
            }else {
                ResultObject oo =new ResultObject();
                oo.code=401;
                oo.message="登录密码必须填写";
                return oo;
            }
        }

        Object rfid = param.get("rfid");
        Object m_rfid = param.get("m_rfid");
        if(m_rfid != null && rfid==null) rfid = m_rfid;
        if(rfid != null && m_rfid==null) m_rfid = rfid;
        ResultObject resultObject = new ResultObject();
        Object locate = param.get("locate");
        Object m_code = param.get("m_code");
        Object sys_code = param.get("sys_code");
        // param.put("rfid",m_rfid.toString());
        if(m_code != null && !m_code.equals("") && m_rfid != null && !m_rfid .equals("")){
            Map<String,Object> map = new HashMap<>();
            map.put("rfid",m_rfid.toString());
            map.put("m_code",m_code.toString());
            mallDao.updateMallRfid(map);
        }
        if(rfid != null && !rfid.equals("") ){
            Map<String,Object> map = new HashMap<>();
            map.put("rfid",m_rfid.toString());
            map.put("locate",locate.toString());
            tpDao.updateTPRfid(map);
            Map<String, Object> stringObjectMap = tpDao.selectTPByLocate(locate.toString());
            Map<String,Object> p1 = new HashMap<>();
            p1.put("locate",locate);
            p1.put("tp_code",stringObjectMap.get("TP_CODE"));
            tpDao.updateLocateTP(p1);
        }
        String opCode = getOPCodeUtil.getOPCode();
        TempOp tempOp = new TempOp();
        tempOp.locate = locate.toString();
        if(m_code != null && m_code != null){
            tempOp.m_code = m_code.toString();
        } else {
            tempOp.m_code = "";
        }
        tempOp.sys_code = CustomIdGenerator.generateId("");
        tempOp.op_desc = "OUT";
        tempOp.op_man ="马媛媛";
        tempOp.op_code = opCode;
        tempOp.dt_ins = new Date();
        tempOp.op_exec="0";
//        tempOp.axac_num = 0;
//        tempOp.blur_num = 0;
        //  if (param.get("m_code")!= null)  tempOp.stt_code = param.get("m_code").toString();
        if(param.get("stt_code")!=null && !param.get("stt_code").equals("")){
            tempOp.stt_code = param.get("stt_code").toString();
        }
        if(tempOp.stt_code == null){
            if(sys_code != null){
                tempOp.stt_code = sys_code.toString();
            }else {
                tempOp.stt_code = "";
            }
        }
        if(param.get("m_cubage")!=null && !param.get("m_cubage").equals("")){
            tempOp.op_num = param.get("m_cubage").toString();
        }else {
            tempOp.op_num = "0";
        }
        tempOpDao.save(tempOp);

        String rtn = getOPCodeUtil.callSelProcedure(opCode);
        if(rfid==null && m_rfid==null){
            locateDao.updateTPState(tempOp.locate);
            locateDao.delTempSttJob(tempOp.stt_code);
            locateDao.delTempOp(tempOp.op_code);
        }
        resultObject.message="出库成功";
        return resultObject;
    }

    @RequestMapping("test/getEmptyLocate2")
    public Object getEmptyLocate2(){
        List<Locate> emptyLocate2 = locateDao.getEmptyLocate2();
        List<Locate> collect = emptyLocate2.stream().filter(locate -> locate.locate.indexOf("-01") > 0)
                .collect(Collectors.toList());
        if(collect!=null && collect.size()>0) {
            return collect.get(0);
        }else {
            List<Locate> collect2 = emptyLocate2.stream().filter(locate -> locate.locate.indexOf("-02") > 0)
                    .collect(Collectors.toList());
            if(collect2!=null && collect2.size()>0) {
                return collect2.get(0);
            }else {
                List<Locate> collect3 = emptyLocate2.stream().filter(locate -> locate.locate.indexOf("-03") > 0)
                        .collect(Collectors.toList());
                if(collect3!=null && collect3.size()>0){
                    return collect3.get(0);
                }else {
                    List<Locate> collect4 = emptyLocate2.stream().filter(locate -> locate.locate.indexOf("-04") > 0)
                            .collect(Collectors.toList());
                    if(collect4!=null && collect4.size()>0){
                        return collect4.get(0);
                    }else {
                      return emptyLocate2.get(0);
                    }
                }
            }
        }

    }

    @RequestMapping("test/saveRealTempOP")
    public Object saveRealTempOP(){
        TempOp tempOp = new TempOp();
        String opCode = getOPCodeUtil.getOPCode();
        tempOp.sys_code = CustomIdGenerator.generateId("");
        tempOp.locate = "001-002-01";
        tempOp.op_desc = "TP";
        tempOp.m_code = "T15-QDBBT-54";
        tempOp.op_man ="马媛媛";
        tempOp.op_code = opCode;
        tempOp.dt_ins = new Date();
        tempOp.op_exec="0";
        tempOp.stt_code = "09";
        tempOpDao.save(tempOp);
        tempOpDao.saveRealTempOp(tempOp);
      //  dbDirectorService.saveRealTempOP(tempOp);
        return tempOp;
    }

    @RequestMapping("test/delTempSttJob")
    public Object delTempSttJob(){
        int i = locateDao.delTempSttJob("01");
        return i;
    }

    @RequestMapping("test/updateTPState")
    public Object updateLocateTP(){
        int i = locateDao.updateTPState("001-002-01");
        return i;
    }

    @RequestMapping("test/delTempOP")
    public Object delTempOP(){
        int i = locateDao.delTempOp("000000000387");

        return i;
    }

    @RequestMapping("test/getMCodeByTpCode")
    public Object getMCodeByTpCode(){
        List<MallVO> mCodeByTpCode = tempOpDao.getMCodeByTpCode("001-001-02");
        return mCodeByTpCode;
    }

    @RequestMapping("test/findMallByCode")
    public Object findMallByCode(){

        return mallDao.findMallByCode("T19C-QMNB");
    }

    @RequestMapping("test/getItemCodeByMCode")
    public Object getItemCodeByMcode(@RequestParam("m_code") String m_code){
        Map<String, Object> itemCodeByMcode = locateDao.getItemCodeByMcode(m_code);
        return itemCodeByMcode;
    }

    public Object getSttDetail(@RequestParam("stt_code") String stt_code){
        List<SttVO> sttVOS = tempOpDao.sttJobDetail(stt_code);
        for(int i=0;i<sttVOS.size();i++){
            SttVO sttVO = sttVOS.get(i);

        }
        return null;
    }
}
